<?php
/*
采购退货单
*/
declare(strict_types=1);

namespace Modules\Purchasing\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Purchasing\Models\Purchasewarehousing;
use Modules\Purchasing\Models\PurchasewarehousingAtached;
use Illuminate\Http\Request;
use Modules\Purchasing\Models\Barcodeprinting;
use Modules\Purchasing\Models\Supplier;
use Modules\Purchasing\Models\Supplierschedule;
use Illuminate\Support\Facades\DB;
use Modules\Purchasing\Models\Provisional;
use Modules\Purchasing\Models\Provisionalschedule;
use Catch\Exceptions\FailedException;
use Modules\Purchasing\Models\Purchaseorder;
use Modules\Purchasing\Models\Purchaseorderschedule;
use Modules\Warehouse\Models\Storagelocation;
use Modules\Purchasing\Models\Inspectionlist;
use Modules\Purchasing\Models\Inspectionlistcheck;
use extend\services\warehouse\{warehouseService};//引入自定义类
use Modules\Purchasing\Http\Repositories\PurchasereturnReget;
use Modules\Purchasing\Models\Purchasereturn;
use Modules\Purchasing\Models\PurchasereturnAttached;


class PurchasereturnController extends Controller
{
    public function __construct(
        protected readonly Purchasereturn $model
    ){}
    

    /**
     * @param Request $request
     * @return mixed
     */
    public function index(): mixed
    {

        $data = $this->model->getList();
        foreach($data as $k=>$v){
            $data[$k]['creator_id'] = isset($datauser[$v['creator_id']])?$datauser[$v['creator_id']]['username']:'';
            $data[$k]['modifier'] = isset($datauser[$v['modifier']])?$datauser[$v['modifier']]['username']:'';
            $data[$k]['state'] = $v['state']==1?'已审核':'未审核'; 
            $data[$k]['writestate'] = $v['writestate']==1?'回写成功':''; 
        }
        return $data;
    }

    /**数据提交解接口
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        $data = $request->all();
        $data['creator_id'] = $this->getLoginUserId();//当前登录用户名称
        return PurchasereturnReget::create($data);
    }

    /**数据详情
     * @param $id
     * @return mixed
     */
    public function show($id,Request $request)
    {
        
        try {
            $data = $request->all();
            $purchasereturn = $this->model->select('id','returnnumber','state','remark','writestate','incomingtype','fbillno','source')->where('id',$id)->first()->toArray();
           
            if(empty($purchasereturn)) throw new FailedException('参数出错');

            $datares = PurchasereturnReget::showdata($purchasereturn,$data);
            $purchasereturn['state'] = $purchasereturn['state']==1?'已审核':'未审核'; 
            $purchasereturn['writestate'] = $purchasereturn['writestate']==1?'回写成功':''; 
            $purchasereturn['details'] = $datares;
            return  ['code'=>1000,'message'=>'请求成功','data'=>$purchasereturn];
        } catch (\Exception $e) {
            DB::rollBack();
            throw new FailedException('服务器飞出地球了请联系技术找回');
        }
    }

    /**数据修改接口
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function updatedata($id, Request $request)
    {
      
       $purchasewarehousing_id =  PurchasereturnAttached::select('purchasereturn_id','id')->where('id',$id)->first();
        if(empty($purchasewarehousing_id)){
            throw new FailedException('数据出错');
        }
       $data =  $this->model->where('id',$purchasewarehousing_id['purchasereturn_id'])->where('state',1)->first();
        if(!empty($data)){
            throw new FailedException('数据已审核无法修改订单已审核');
        }
        return PurchasereturnReget::createUpdate($purchasewarehousing_id, $request->all());
        
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        return PurchasereturnReget::delete($id);
    }


    /*
    *采购退货货单PDA扫描接口
    * @param $id
    * @return mixed
    */
    public function purchaseininbound($code)
    {
        return PurchasereturnReget::checkPdaApi($code);
    }

    /*
    * 采购退货单审核接口
    * @param $id   
    * @return mixed
    */
    public function purchaseexamine($id)
    {
        
        return PurchasereturnReget::auditupdate($id);
    }


    /*
    *组织间退货 采购退货货单PDA扫描接口
    * @param $id
    * @return mixed
    */
    public function intertissueturn($code)
    {
       
        return PurchasereturnReget::intertissueturnData($code);
    }


    /*
    *组织间退货 采购退货货单PDA扫描接口-数据添加接口
    * @param $id
    * @return mixed
    */
    public function returndata(Request $request)
    {
        $data = $request->all();
        $data['creator_id'] = $this->getLoginUserId();//当前登录用户名称
        return PurchasereturnReget::returnDataAdd($data);
    }



}
